package org.jeecg.modules.front.controller;

import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.front.entity.req.WxUserRegisterReq;
import org.jeecg.modules.front.entity.resp.BusUserResp;
import org.jeecg.modules.front.util.UserLoginUtils;
import org.jeecg.modules.front.service.BusUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@Slf4j
@Api(tags="手机端用户登陆")
@RestController
@RequestMapping("/mobile/user")
public class UserController {
    @Autowired
    private UserLoginUtils userLoginUtils;
    @Autowired
    private BusUserService userService;

    @ApiOperation(value = "微信授权登录/注册", notes = "微信授权登录/注册")
    @PostMapping(value = "/wxRegisterAndLogin")
    public Result wxAppRegisterAndLogin(@RequestBody WxUserRegisterReq req, HttpServletRequest request) {
        log.info("微信小程序登录/注册: WxUserController  wxUserRegisterAndLogin req={}", JSON.toJSONString(req));
        Result<BusUserResp> result = userService.wxAppUserLogin(req);
        //当失败时
        if(!result.isSuccess()){
            return result;
        }
        BusUserResp busUserResp = result.getResult();
        return userLoginUtils.getUserInfo(busUserResp,request);
    }
}
